import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { LowcodeGroovyApi } from '#/api/lowcode/groovy';

import { z } from '#/adapter/form';

/** 脚本分类选项 */
export const SCRIPT_CATEGORIES = [
  { label: '数据处理', value: 'data' },
  { label: '业务逻辑', value: 'business' },
  { label: '工具函数', value: 'util' },
  { label: '定时任务', value: 'job' },
  { label: '接口集成', value: 'api' },
  { label: '其他', value: 'other' },
];

/** 新增/修改 Groovy 脚本的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'scriptName',
      label: '脚本标识',
      component: 'Input',
      componentProps: {
        placeholder: '请输入脚本唯一标识',
        maxlength: 50,
      },
      rules: z.string().min(1, '脚本标识不能为空').max(50, '脚本标识不能超过50个字符'),
    },
    {
      fieldName: 'displayName',
      label: '显示名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入脚本显示名称',
        maxlength: 100,
      },
      rules: z.string().min(1, '显示名称不能为空').max(100, '显示名称不能超过100个字符'),
    },
    {
      fieldName: 'category',
      label: '脚本分类',
      component: 'Select',
      componentProps: {
        placeholder: '请选择脚本分类',
        options: SCRIPT_CATEGORIES,
      },
      rules: z.string().min(1, '请选择脚本分类'),
    },
    {
      fieldName: 'description',
      label: '脚本描述',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入脚本描述',
        maxlength: 500,
        rows: 3,
        showCount: true,
      },
      rules: z.string().max(500, '脚本描述不能超过500个字符').optional(),
    },
    {
      fieldName: 'scriptContent',
      label: '脚本内容',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入Groovy脚本代码',
        rows: 10,
        style: 'font-family: "JetBrains Mono", "Fira Code", Consolas, monospace;',
      },
      rules: z.string().min(1, '脚本内容不能为空'),
      help: '支持Groovy语法，可以使用内置的工具类和服务',
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        options: [
          { label: '启用', value: 1 },
          { label: '禁用', value: 0 },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(1),
      defaultValue: 1,
    },
  ];
}

/** 脚本编辑器的表单配置 */
export function useEditorFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'scriptName',
      label: '脚本标识',
      component: 'Input',
      componentProps: {
        placeholder: '请输入脚本唯一标识',
        maxlength: 50,
      },
      rules: z.string().min(1, '脚本标识不能为空').max(50, '脚本标识不能超过50个字符'),
    },
    {
      fieldName: 'displayName',
      label: '显示名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入脚本显示名称',
        maxlength: 100,
      },
      rules: z.string().min(1, '显示名称不能为空').max(100, '显示名称不能超过100个字符'),
    },
    {
      fieldName: 'category',
      label: '分类',
      component: 'Select',
      componentProps: {
        placeholder: '请选择脚本分类',
        options: SCRIPT_CATEGORIES,
        style: { width: '150px' },
      },
      rules: z.string().min(1, '请选择脚本分类'),
    },
    {
      fieldName: 'description',
      label: '描述',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入脚本描述',
        maxlength: 500,
        rows: 2,
        showCount: true,
      },
      rules: z.string().max(500, '脚本描述不能超过500个字符').optional(),
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(handleStatusChange?: (newStatus: number, row: LowcodeGroovyApi.GroovyScript) => Promise<boolean | undefined>): VxeTableGridOptions<LowcodeGroovyApi.GroovyScript>['columns'] {
  return [
    {
      type: 'checkbox',
      width: 60,
    },
    {
      field: 'id',
      title: 'ID',
      width: 80,
    },
    {
      field: 'scriptName',
      title: '脚本标识',
      width: 120,
    },
    {
      field: 'displayName',
      title: '脚本名称',
      minWidth: 150,
    },
    {
      field: 'category',
      title: '分类',
      width: 100,
    },
    {
      field: 'description',
      title: '描述',
      minWidth: 200,
      showOverflow: 'tooltip',
    },
    {
      field: 'version',
      title: '版本',
      width: 80,
      align: 'center',
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      align: 'center',
      cellRender: {
        name: 'CellSwitch',
        props: {
          disabled: !handleStatusChange,
          checkedValue: 1,
          unCheckedValue: 0,
          onChange: handleStatusChange,
        },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'updateTime',
      title: '更新时间',
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 240,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 获取分类对应的颜色 */
export function getCategoryColor(category: string) {
  const colors: Record<string, string> = {
    data: 'blue',
    business: 'green',
    util: 'orange',
    job: 'purple',
    api: 'cyan',
    other: 'default',
  };
  return colors[category] || 'default';
}

/** 获取分类显示文本 */
export function getCategoryText(category: string) {
  const categoryMap: Record<string, string> = {
    data: '数据处理',
    business: '业务逻辑',
    util: '工具函数',
    job: '定时任务',
    api: '接口集成',
    other: '其他',
  };
  return categoryMap[category] || category;
}

/** 搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'scriptName',
      label: '脚本标识',
      component: 'Input',
      componentProps: {
        placeholder: '请输入脚本标识',
      },
    },
    {
      fieldName: 'displayName',
      label: '脚本名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入脚本名称',
      },
    },
    {
      fieldName: 'category',
      label: '脚本分类',
      component: 'Select',
      componentProps: {
        placeholder: '请选择脚本分类',
        options: SCRIPT_CATEGORIES,
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        placeholder: '请选择状态',
        options: [
          { label: '启用', value: 1 },
          { label: '禁用', value: 0 },
        ],
      },
    },
  ];
}